package com.wx.admin.service.impl;

import com.drew.lang.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.admin.service.SysDictService;
import com.wx.common.utils.StringUtils;
import com.wx.mapper.SysDictMapper;
import com.wx.pojo.SysDict;
import com.wx.pojo.SysDictExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.xml.ws.ServiceMode;
import java.util.Date;
import java.util.List;

@Service
public class SysDictServiceImpl implements SysDictService {

    @Autowired
    private SysDictMapper sysDictMapper;

    public PageInfo<SysDict> getPageList(String keyword,Date startTime,Date endTime, int pageIndex,int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        SysDictExample sysDictExample = new SysDictExample();
        SysDictExample.Criteria criteria = sysDictExample.createCriteria();
        if(!StringUtils.isBlank(keyword))
        {
            criteria.andKeywordLike("%"+keyword+"%");
        }
        if (startTime != null) {
            criteria.andCreateDateGreaterThan(startTime);
        }
        if (endTime != null)
        {
            criteria.andCreateDateLessThan(endTime);
        }

        sysDictExample.setOrderByClause("create_date desc");
        List<SysDict> list = sysDictMapper.selectByExample(sysDictExample);
        PageInfo<SysDict> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    public List<SysDict> getList(int top)
    {
        SysDictExample dictExample = new SysDictExample();
        dictExample.setPageIndex(0);
        dictExample.setPageSize(top);
        dictExample.setOrderByClause("create_date Desc");
        List<SysDict> list = sysDictMapper.selectByExample(dictExample);
        return  list;
    }

    public SysDict getById(Long id)
    {
        SysDict sysDict = sysDictMapper.selectByPrimaryKey(id);
        return sysDict;
    }

    public int update(SysDict dict)
    {
        return sysDictMapper.updateByPrimaryKey(dict);

    }
    public SysDict insert(String keyword,String value, String remarks,String delFlag)
    {
        SysDict sysDict = new SysDict();
        sysDict.setCreateBy("admin");
        sysDict.setCreateDate(new Date());
        sysDict.setDelFlag(delFlag);
        sysDict.setDescription("des");
        sysDict.setKeyword(keyword);
        sysDict.setLable("lable");
        sysDict.setParentId(10l);
        sysDict.setRemarks(remarks);
        sysDict.setSort(11);
        sysDict.setType("0");
        sysDict.setUpdateDate(new Date());
        sysDict.setUpdateBy("update by");
        sysDict.setValue(value);
        sysDictMapper.insert(sysDict);
        return sysDict;
    }

    public void delete(long id)
    {
        sysDictMapper.deleteByPrimaryKey(id);
    }
}
